package com.cn.wanxi.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.UserService;
import com.cn.wanxi.service.impl.UserServiceImpl;
import com.cn.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 新增，修改，删除用post
 * 查询用get
 * 如果是用流的方式，或者设计到安全问题 用post
 */
@WebServlet("/back/user/add")
public class UserAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.得到签到传递过来的数据
        String username = req.getParameter("username");
        String address = req.getParameter("address");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");
        String sex = req.getParameter("sex");
        String birthday = req.getParameter("birthday");
        String hobby = req.getParameter("hobby");
        String enable = req.getParameter("enable");
//        String[] hobbys=req.getParameterValues("hobby");
//        3.将这些封装成对象
        UserModel model = new UserModel();

        model.setName(Tool.nullToStr(username));
        model.setPassword(Tool.nullToStr(password));
        model.setPhone(Tool.nullToStr(phone));
        model.setSex(Tool.nullToStr(sex));
        model.setBirthday(Tool.nullToStr(birthday));
        model.setAddress(Tool.nullToStr(address));
        model.setEnable(Tool.strToInt(enable));
        model.setHobby(Tool.nullToStr(hobby));
//        4.调用服务逻辑层
        UserService userService = new UserServiceImpl();
//        5.得到结果信息
        ResultModel resultModel = userService.add(model);
//        6.返回前端
        resp.getWriter().println(JSONObject.toJSONString(resultModel));

    }
}
